import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import com.sun.faces.util.MessageFactory;

/**
 * Class CPValidation, validator JSF qui valide le code postal d'un champ.
 * 
 */
public class CpValidation implements Validator
{
	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException
	{
		String enteredCp = String.valueOf((Integer) value);
		Pattern p = Pattern.compile("[0-9]+");
		Matcher m = p.matcher(enteredCp);
		boolean matchFound = m.matches();

		if (!matchFound)
			throw new ValidatorException(MessageFactory.getMessage(context,
					"error_cpmalformed"));
	}

}
